--
-- Author: shunguo.chen
-- Date: 2019-07-02 11:14:27
--
-- AvatarShopPanel 理发店选择panel
--

local AvatarShopRender = import(".renders.AvatarShopRender")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AvatarShopPanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

-- 
function ClassRef:init(data)
    self.super.init(self, data)

    self._panelInitSize = self._ui.Panel:getContentSize()
    self:setContentSize(self._panelInitSize)

    self._ui.ScrollView:removeAllChildren()
    self._listInitSize = self._ui.ScrollView:getContentSize()

    self._renders = {}

    local render = self:_getRender(1)
    self._renderWidth, self._renderHeight = render:getContentSize2()
end

function ClassRef:_onClickRender(op, render)
    if op == "get_resource" then
        executeFunc(self._onOperate, "get_resource", render:getData())
    elseif op == "item_select" then
        if self._curRender ~= render then
            self:_setCurRender(render)
            executeFunc(self._onOperate, "item_select", render:getData())
        end
    end
end

function ClassRef:_setCurRender(render)
    if self._curRender then
        self._curRender:setSelected(false)
    end
    render:setSelected(true)
    self._curRender = render
end

function ClassRef:_getRender(idx)
    local render = self._renders[idx]
    if not render then
        render = AvatarShopRender.create()
        render:onBtnClick(handler(self, self._onClickRender))
        self._ui.ScrollView:addChild(render)
        self._renders[#self._renders + 1] = render
    end
    render:setVisible(true)
    return render
end

-- items {AvatarShopItem, ...}
-- initId 初始id，也就是当前实际穿着的id
-- curId 当前试穿的id
function ClassRef:refreshContent(items, initId, curId)
    -- dump(items, "refreshContent items")
    -- print("initId, curId", initId, curId)

    -- hide useless render
    local itemLen = #items
    if #self._renders > #items then
        for i=itemLen+1, #self._renders do
            self._renders[i]:setVisible(false)
        end
    end

    local listHeight = math.ceil(itemLen / 2) * self._renderHeight
    local listSize = cc.size(self._listInitSize.width, listHeight)
    -- self._ui.ScrollView:setContentSize(listSize)
    local panelSize = cc.size(self._panelInitSize.width,
        self._panelInitSize.height + listSize.height - self._listInitSize.height)
    self._ui.Panel:setContentSize(panelSize)
    self:setContentSize(panelSize)
    ccui.Helper:doLayout(self._ui.Panel)
    self._ui.arrow:setPositionY(panelSize.height/2)
    self._ui.ScrollView:setInnerContainerSize(listSize)

    local initPos = cc.p(self._renderWidth/2, listSize.height - self._renderHeight/2)
    local render, id, curIdx
    for i,item in ipairs(items) do
        render = self:_getRender(i)
        render:setData(item)
        id = item:getId()
        render:setEquiped(id == initId)
        render:setSelected(id == curId)
        render:setPositionX(initPos.x + (i-1) % 2 * self._renderWidth)
        render:setPositionY(initPos.y - math.floor((i - 1) / 2) * self._renderHeight)
        if id == curId then
            curIdx = i
        end
    end
    self:_setCurRender(self._renders[curIdx or 1])
end

function ClassRef:refreshPosY(posY)
    local _, height = self:getContentSize2()
    local offsetY = 0
    local screenHeight = display.height
    if (posY + height/2) > (screenHeight - 10) then
        offsetY = screenHeight - 10 - (posY + height/2)
    elseif (posY - height/2) < 10 then
        offsetY = 10 - (posY - height/2)
    end

    self:setPositionY(posY + offsetY)
    self._ui.arrow:setPositionY(height/2 - offsetY)
end

function ClassRef:setName(name)
    self._ui.name:setString(name)
end

return ClassRef
